#include "programset.h"
#include "programengine.h"
#include "regdata.h"
#include "modbusreadinstruction.h"
#include "modbusreadinstructionlogic.h"
#include "modbusmanager.h"
#include "GeneralDefine.h"
#include "messagelog.h"

extern ModbusManager *modbusManager;

ModbusReadInstructionLogic::ModbusReadInstructionLogic(int robotIdIn, ProgramSet *programSetIn, ProgramEngine *programEngineIn, RegData *regDataIn)
{
    robotId = robotIdIn;
    programSet = programSet;
    programEngine = programEngineIn;
    regData = regDataIn;
}

E_PROGRAM_LOOP_RESULT ModbusReadInstructionLogic::startModbusRead(int programNumIn, const ModbusReadInstruction &instructionIn, int lineIndex)
{
    Q_UNUSED(lineIndex);
    if(instructionIn.instructionType != MODBUS_READ)
    {
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12081, robotId, programEngine->getRunningLineFloat());
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }

    // Modbus-Rs485主机模式
    if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_RS485_MASTER)
    {
        if(1!=readModbusRtuClient(instructionIn.busId.toInt(),instructionIn.modbusReadDataRegisterIndex,
                                  instructionIn.modbuReadRegAddress))
        {
            addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12090, robotId, programEngine->getRunningLineFloat());
            return E_PROGRAM_LOOP_RESULT_ERROR;
        }
    }
    // Modbus-Rs485从机模式
    else if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_RS485_SLAVE)
    {
        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12091, robotId, programEngine->getRunningLineFloat());
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }
    // Modbus-TCP主机模式
    else if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_INTERNET_MASTER)
    {
        if(1!=readModbusTcpClient(instructionIn.busId,instructionIn.modbusReadDataRegisterIndex,
                                  instructionIn.modbuReadRegAddress))
        {
            addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12090, robotId, programEngine->getRunningLineFloat());
            return E_PROGRAM_LOOP_RESULT_ERROR;
        }

    }
    // Modbus-TCP从机模式
    else if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_INTERNET_SLAVE)
    {

        if(1!=readModbusTcpServer(instructionIn.busId.toInt(),instructionIn.modbusReadDataRegisterIndex,
                                  instructionIn.modbuReadRegAddress))
        {
            addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12090, robotId, programEngine->getRunningLineFloat());
            return E_PROGRAM_LOOP_RESULT_ERROR;
        }
    }
    // ERROR
    else if(instructionIn.modbusType == E_MODBUS_SPEC_TYPE_ERROR)
    {

        addMsg( ENUM_MSG_ERROR, "ProgramEngine", "ProgramEngine",  12082, robotId, programEngine->getRunningLineFloat());
        return E_PROGRAM_LOOP_RESULT_ERROR;
    }


    programEngine->calculateNextLineToRun(programNumIn);
    return E_PROGRAM_LOOP_RESULT_NORMAL;

}

int ModbusReadInstructionLogic::readModbusTcpServer( int portIn,int robotRegisterIndex,int modbusRegisterAddress)
{
    int regValue;
    if(1==modbusManager->readModbusRegister_Server(1,portIn,modbusRegisterAddress,regValue))
    {
        regData->setRRegValue(robotRegisterIndex-1,regValue);
        return 1;
    }
    return -1;

}

int ModbusReadInstructionLogic::readModbusTcpClient(QString ipAndPort, int robotRegisterIndex,
                                                    int modbusRegisterAddress)
{
    int regValue;
    if(1==modbusManager->readModbusRegister_TcpClient(ipAndPort,modbusRegisterAddress,regValue))
    {
        regData->setRRegValue(robotRegisterIndex-1,regValue);
        return 1;
    }
    return -1;
}

int ModbusReadInstructionLogic::readModbusRtuClient(int portIn, int robotRegisterIndex,
                                                    int modbusRegisterAddress)
{
    int regValue;
    if(1==modbusManager->readModbusRegister_rtuClient(portIn,modbusRegisterAddress,regValue))
    {
        regData->setRRegValue(robotRegisterIndex-1,regValue);
        return 1;
    }
    return -1;
}

void ModbusReadInstructionLogic::addMsg(int messageLevel, string componentName, string messageType, int messageCode, int robotId, int parameter1, int parameter2, int parameter3, int parameter4, QString message)
{
    Q_UNUSED(parameter2);
    Q_UNUSED(parameter3);
    Q_UNUSED(parameter4);

    Message tmpMsg;
    tmpMsg.MessageLevel = messageLevel;
    tmpMsg.componentClassName = componentName;
    tmpMsg.messageType = messageType;
    tmpMsg.robotId = robotId;
    tmpMsg.messageCode = messageCode;

    QString infomationStr;

    switch(messageCode)
    {
    case 12091:
    {
         infomationStr = QObject::tr("机器人:") +
             QString::number(robotId) +
             QObject::tr("第") +
             QString::number(parameter1+1) +
             QObject::tr("行运行出现异常:暂时不支持的参数模式!")+message;
         break;
    }
       case 12090:
       {
            infomationStr = QObject::tr("机器人:") +
                QString::number(robotId) +
                QObject::tr("第") +
                QString::number(parameter1+1) +
                QObject::tr("行运行出现异常:ModbusRead数据读取失败!")+message;
            break;
       }
        default:
       {
            break;
       }
    }
    tmpMsg.MessageInformation = infomationStr.toStdString();
    MessageLog::getInstance()->addMessage(tmpMsg);
}
